
//
//  WEWindow.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/2/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation


class WEWindow: NSWindow {
    
    var identifier_navtiveControl = NSToolbarItem.Identifier.init(rawValue: "identifier_navtiveControl")
    var identifier_stitle = NSToolbarItem.Identifier.init(rawValue: "identifier_stitle")
    
    var webView:WKWebView? {
        didSet {
            webView?.navigationDelegate = self
        }
    }
    
    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
        super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
        
        self.collectionBehavior = NSWindow.CollectionBehavior.fullScreenNone
        self.styleMask = [.titled,.closable,.resizable]
        self.isMovableByWindowBackground = true
        
        self.toolbar = self.customToolbar
        /*
        if #available(OSX 10.13, *) {
            self.toolbar = NSToolbar.init()
            self.reTitleView()
        } else {
            // Fallback on earlier versions
        }
        */
        
    }
    
    func frame(for size: NSSize) -> NSRect {
        var newFrame: NSRect = frame
        newFrame.origin.x += (newFrame.size.width - size.width) / 2
        newFrame.origin.y += newFrame.size.height - size.height
        newFrame.size = size
        print(newFrame)
        return newFrame
    }
    
    override func setFrame(_ frameRect: NSRect, display flag: Bool) {
        //let rect = self.inLiveResize ? self.frame(for: frameRect.size):frameRect
        super.setFrame(frameRect, display: flag)
    }
    
    lazy var customToolbar: NSToolbar = {
        let tool = NSToolbar.init(identifier: "WEWindow")
        tool.allowsUserCustomization = true
        tool.autosavesConfiguration = true
        tool.displayMode = .iconOnly
        tool.sizeMode = .small
        tool.delegate = self
        return tool
    }()
    
    lazy var sTitle: NSTextField = {
        let lab =  NSTextField.init(frame: NSRect.init(x: 0, y: 0, width: 100, height: 24))
        lab.isBordered = false
        lab.isEditable = false
        lab.textColor = NSColor.black
        lab.font = NSFont.systemFont(ofSize: 22)
        lab.stringValue = "直播设置".localized()
        lab.maximumNumberOfLines = 1
        lab.alignment = .center
        lab.isHidden = false
        lab.sizeToFit()
        return lab
    }()
    
    
    func reTitleView() {
        //添加自定义views
        self.addCustemViews()
    }
    
    func addCustemViews() {
        ////控制器
        //self.sTitleView?.addSubview(self.webControl)
        self.navControl.frame = CGRect.init(x: -100, y: 0, width: (20+15)*3-15, height: 40)
        /*
        self.navControl.snp.mzakeConstraints { (make) in
            make.width.equalTo((32+5)*3-5)
            make.height.equalToSuperview()
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
        }
        */
        
        self.navControl.addSubview(reloadButton)
        reloadButton.snp.makeConstraints { (make) in
            make.size.equalTo(reloadButton.image!.size)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(0)
        }
        
        self.navControl.addSubview(forworButton)
        forworButton.snp.makeConstraints { (make) in
            make.size.equalTo(reloadButton.image!.size)
            make.centerY.equalToSuperview()
            make.right.equalTo(reloadButton.snp.left).offset(-15)
        }

        self.navControl.addSubview(backButton)
        backButton.snp.makeConstraints { (make) in
            make.size.equalTo(reloadButton.image!.size)
            make.centerY.equalToSuperview()
            make.right.equalTo(forworButton.snp.left).offset(-15)
        }
        
    }
    
    //获取 titleView
    lazy var sTitleView: NSView? = {
        let themeView = self.contentView?.superview
        let titleView  = themeView?.subviews[1]
        titleView?.wantsLayer = true
        titleView?.layer?.backgroundColor = color_gray(245).cgColor
        return titleView
    }()
    
    lazy var sLine: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(220).cgColor
        return view
    }()
    
    lazy var navControl: NSView = {
        let view =  NSView()
        view.layer?.backgroundColor = color_gray(200).cgColor
        return view
    }()
    
    lazy var backButton: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.init(named: NSImage.Name.init("icon_navigate_back"))!
        btn.action = #selector(backBtnAction)
        btn.target = self
        return btn
    }()
    
    lazy var forworButton: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.init(named: NSImage.Name.init("icon_navigate_forwor"))!
        btn.action = #selector(fowBtnAction)
        btn.target = self
        return btn
    }()
     
    lazy var reloadButton: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.init(named: NSImage.Name.init("icon_navigate_refresh"))!
        btn.action = #selector(reloadBtnAction)
        btn.target = self
        return btn
    }()
    
    @objc func backBtnAction() {
        if self.webView != nil {
            if self.webView!.canGoBack == true {
                self.webView!.goBack()
            }
        }
        
    }
    @objc func fowBtnAction() {
        if self.webView != nil {
            if self.webView!.canGoForward == true {
                self.webView!.goForward()
            }
        }
        
    }
    
    @objc func reloadBtnAction() {
        if self.webView != nil {
            self.webView!.reload()
        }
    }
    
    func reloadWebButton() {
        if self.webView != nil {
            self.webView?.reload()
        }
    }
}

extension WEWindow:WKNavigationDelegate {
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.reloadWebButton()
    }
}


extension WEWindow:NSToolbarDelegate {
    func toolbarItemIdentifiers() -> [NSToolbarItem.Identifier] {
        return [
            NSToolbarItem.Identifier.space,
            identifier_navtiveControl,
            NSToolbarItem.Identifier.flexibleSpace,
        ]
    }
    func toolbarDefaultItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItemIdentifiers()
    }
    
    func toolbarAllowedItemIdentifiers(_ toolbar: NSToolbar) -> [NSToolbarItem.Identifier] {
        return self.toolbarItemIdentifiers().removingDuplicates()
    }
    
    func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
        let toolbarItem = NSToolbarItem.init(itemIdentifier: itemIdentifier)
        if itemIdentifier.rawValue.isEqual(identifier_navtiveControl.rawValue) {
            self.addCustemViews()
            toolbarItem.view = self.navControl
            return toolbarItem
        }else if itemIdentifier.rawValue.isEqual(identifier_stitle.rawValue){
            toolbarItem.view = self.sTitle
            return toolbarItem
        }else {
            return nil
        }
        
    }
}
